package util;

import java.util.ArrayList;

public class IntList extends Parsable<IntList> {
	ArrayList<Int> val;
	public IntList() {
		val = null;
	}

	public IntList(ArrayList<Int> in) {
		if(in != null)
		val = in;
	}

	public ArrayList<Int> get() {
		return val;
	}

	@Override
	public IntList parse(String str) {
		if(str == null || str.isEmpty())
			return new IntList();
//		if(str.isEmpty())
//			return new IntList();
		//check legal
		String tmp = str.substring(1, str.lastIndexOf("]"));
		if(tmp.isEmpty())
			return new IntList();
		String[] array = tmp.split(",");
		if(val == null) val = new ArrayList<Int>();
		val.clear();
		for(String s: array){
			val.add(new Int(Integer.parseInt(s)));
		}

		return new IntList(val);
	}

	@Override
	public String toString() {
		if(val == null){
			return "[]";
		}
		StringBuffer buff = new StringBuffer();
		buff.append("[");
		int counter =0;
		for(Int str : val){
			buff.append(str.toString());
			buff.append(",");
			counter ++;
		}
		//buff.substring(buff.lastIndexOf(","), buff.length()-1);
		buff.append("]");
		if(counter >0)
		buff.delete(buff.lastIndexOf(","), buff.length()-1);
		
		return buff.toString();
	}
}
